package com.ysg.widget.table;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.ysg.R;

/**
 * 表格单元格
 */
public class YTable extends LinearLayout {
    private RelativeLayout rlTableTitle;
    private RecyclerView rvTableTitle;
    private RecyclerView rvTableContentLeft;
    private RecyclerView rvTableContentRight;

    public YTable(Context context) {
        super(context);
        init(context);
    }

    public YTable(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context){
        View view = View.inflate(context, R.layout.widget_table, this);
        rlTableTitle = view.findViewById(R.id.rl_table_title);
        rvTableTitle = view.findViewById(R.id.rv_table_title);
        rvTableContentLeft = view.findViewById(R.id.rv_table_content_left);
        rvTableContentRight = view.findViewById(R.id.rv_table_content_right);
        TableTitleHorizontalScrollView svTableTitleRight = view.findViewById(R.id.sv_table_title_right);
        TableContentHorizontalScrollView svTableContentRight = view.findViewById(R.id.sv_table_content_right);

        LinearLayoutManager titleLayoutManager = new LinearLayoutManager(getContext());
        titleLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
        LinearLayoutManager leftLayoutManager = new LinearLayoutManager(getContext());
        LinearLayoutManager rightLayoutManager = new LinearLayoutManager(getContext());

        rvTableTitle.setLayoutManager(titleLayoutManager);
        rvTableContentLeft.setLayoutManager(leftLayoutManager);
        rvTableContentRight.setLayoutManager(rightLayoutManager);

        // 左右滑动同步
        svTableContentRight.setLinkScrollChangeListener(new TableContentHorizontalScrollView.LinkScrollChangeListener() {
            @Override
            public void onScroll(int l, int t) {
                svTableTitleRight.scrollTo(l, t);
            }
        });

        // 左侧ListView滚动时，控制右侧ListView滚动
        rvTableContentLeft.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_DRAGGING){
                    rvTableContentLeft.stopScroll();
                    rvTableContentRight.stopScroll();
                }
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if(recyclerView.getScrollState() != RecyclerView.SCROLL_STATE_IDLE){
                    rvTableContentRight.scrollBy(dx,dy);
                }
            }
        });

        rvTableContentLeft.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                    case MotionEvent.ACTION_POINTER_DOWN:
                        rvTableContentLeft.stopScroll();
                        rvTableContentRight.stopScroll();
                }
                return false;
            }
        });

        rvTableContentRight.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                    case MotionEvent.ACTION_POINTER_DOWN:
                        rvTableContentLeft.stopScroll();
                        rvTableContentRight.stopScroll();
                }
                return false;
            }
        });

        rvTableContentRight.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if(recyclerView.getScrollState() != RecyclerView.SCROLL_STATE_IDLE){
                    rvTableContentLeft.scrollBy(dx,dy);
                }
            }
        });
    }

    /**
     * 这种适合复杂的标题：如标题有点击事件等等
     */
    public void setAdapter(RecyclerView.Adapter leftAdapter, RecyclerView.Adapter rightAdapter, RecyclerView.Adapter titleAdapter){
        rvTableTitle.setAdapter(titleAdapter);
        rvTableContentLeft.setAdapter(leftAdapter);
        rvTableContentRight.setAdapter(rightAdapter);
    }

    /**
     * 这种适合简单的标题
     */
    public void setAdapter(RecyclerView.Adapter leftAdapter, RecyclerView.Adapter rightAdapter, int layoutTitleId){
        View tableTitle = LayoutInflater.from(getContext()).inflate(layoutTitleId, null);
        rlTableTitle.addView(tableTitle);
        rvTableContentLeft.setAdapter(leftAdapter);
        rvTableContentRight.setAdapter(rightAdapter);
    }







}
